package com.game.business.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
        import com.game.common.utils.DateUtils;
import com.game.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.game.business.mapper.TWalletMapper;
import com.game.business.domain.TWallet;
import com.game.business.service.ITWalletService;

/**
 * 余额明细Service业务层处理
 *
 * @author ruoyi
 * @date 2025-10-11
 */
@Service
public class TWalletServiceImpl extends ServiceImpl<TWalletMapper,TWallet>  implements ITWalletService
{
    @Autowired
    private TWalletMapper tWalletMapper;

    /**
     * 查询余额明细
     *
     * @param id 余额明细主键
     * @return 余额明细
     */
    @Override
    public TWallet selectTWalletById(String id)
    {
        return tWalletMapper.selectTWalletById(id);
    }

    /**
     * 查询余额明细列表
     *
     * @param tWallet 余额明细
     * @return 余额明细
     */
    @Override
    public List<TWallet> selectTWalletList(TWallet tWallet)
    {
        return tWalletMapper.selectTWalletList(tWallet);
    }

    /**
     * 新增余额明细
     *
     * @param tWallet 余额明细
     * @return 结果
     */
    @Override
    public int insertTWallet(TWallet tWallet)
    {
                tWallet.setCreateTime(DateUtils.getNowDate());
            return tWalletMapper.insertTWallet(tWallet);
    }

    /**
     * 修改余额明细
     *
     * @param tWallet 余额明细
     * @return 结果
     */
    @Override
    public int updateTWallet(TWallet tWallet)
    {
                tWallet.setUpdateTime(DateUtils.getNowDate());
        return tWalletMapper.updateTWallet(tWallet);
    }

    /**
     * 批量删除余额明细
     *
     * @param ids 需要删除的余额明细主键
     * @return 结果
     */
    @Override
    public int deleteTWalletByIds(String[] ids)
    {
        return tWalletMapper.deleteTWalletByIds(ids);
    }

    /**
     * 删除余额明细信息
     *
     * @param id 余额明细主键
     * @return 结果
     */
    @Override
    public int deleteTWalletById(String id)
    {
        return tWalletMapper.deleteTWalletById(id);
    }


    @Override
    public BigDecimal getTotalMoney(){
        QueryWrapper<TWallet> tWrapper = new QueryWrapper<TWallet>();
        tWrapper.select("COALESCE(SUM(money), 0) as money");
        tWrapper.eq("user_id", SecurityUtils.getUserId());
        TWallet wallet = this.getOne(tWrapper);
        BigDecimal money = wallet.getMoney();
        return money;
    }

    @Override
    public BigDecimal getTotalXhMoney(){
        QueryWrapper<TWallet> tWrapper = new QueryWrapper<TWallet>();
        tWrapper.select("COALESCE(SUM(money), 0) as money");
        tWrapper.eq("user_id", SecurityUtils.getUserId());
        // 金额小于0的
        tWrapper.lt("money", 0);
        TWallet wallet = this.getOne(tWrapper);
        BigDecimal money = wallet.getMoney();
        return money;
    }

}
